import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface AdsModelType {
  namespace: 'ads';
  state: any;
//   effects: {
//     query: Effect;
//   };
  reducers: {
    addCard: ImmerReducer<[]>,
    changeCardInfo: ImmerReducer<[]>,
    changeCardItems: ImmerReducer<[]>,
    initAds: ImmerReducer<[]>
  };
//   subscriptions: { setup: Subscription };
}

const AdsModel: AdsModelType = {
  namespace: 'ads',
  state: [],

//   effects: {
//     *query({ payload }, { call, put }) {},
//   },
  reducers: {
    // 启用 immer 之后
    addCard(state, action) {
        let obj: any = {}
        let NewState = JSON.parse(JSON.stringify(state))
        NewState.push(obj)
        return NewState
    },
    changeCardInfo(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      console.log(action.info.index)
      console.log(NewState[action.info.index - 1])
      NewState[action.info.index - 1].url = action.info.val
      return NewState
    },
    changeCardItems(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState[action.info.index - 1].img = action.info.val
      return NewState
    },
    initAds(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState = action.items
      return NewState
    }
  }
};

export default AdsModel;